//
//  WmcRemarkVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/11.
//

import UIKit
import RxCocoa

// MARK: - 扩展WmcRemarkVC + WmcNavigatorProtocol

extension WmcRemarkVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.remark]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcRemarkVM()
        if let viewModel = parms.context as? WmcRemarkVM {
            vm = viewModel
        }
        let vc = WmcRemarkVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcRemarkVC + WmcVMProtocol

extension WmcRemarkVC: WmcVMProtocol {
    typealias VMType = WmcRemarkVM
}

// MARK: - [WmcRemarkVC]

class WmcRemarkVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.loadData()
    }

    // Internal

    lazy var saveBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "保存")
        return btn
    }()

    lazy var textView: WmcTextView = {
        let textView = WmcTextView()
        return textView
    }()
    
    lazy var historyTitleLabel: UILabel = {
        let label = UILabel()
        return label
    }()

    lazy var tableView: UITableView = {
        let tableView = UITableView.YP.Create(style: .grouped, delegate: self, dataSource: self, registers: [
            YPRegister(WmcRemarkCell.self),
        ])
        tableView.rowHeight = 50
        return tableView
    }()

    override func makeUI() {
        makeNavView(showShadow: false)
        title = "备注"

        navView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-6)
            make.width.equalTo(52.wmc_autoHor)
            make.height.equalTo(32)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        // 备注文字
        textView.text = viewModel.remarkText
        
        addSubview(textView)
        textView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(Wmc_standardEdge.top)
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.height.equalTo(160.wmc_autoVer)
        }
        
        addSubview(historyTitleLabel)
        historyTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(textView.snp.bottom).offset(Wmc_standardEdge.top)
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
        }
        
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(historyTitleLabel.snp.bottom).offset(Wmc_standardEdge.top)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        viewModel.remarkHistoryRelay.skip(1).subscribe(onNext: { [weak self] value in
            guard let weakSelf = self else { return }
            let markStr = " (\(value.count))"
            let attStr = "历史记录\(markStr)".yp.makeAttributed { make in
                make.set(font: 16.yp.uiFont(weight: .medium))
                    .set(foregroundColor:UIColor.wmc_grayH2_323233, range: 0..<4)
                    .set(foregroundColor:UIColor.wmc_mainTheme, range: 4..<markStr.count + 4)
            }
            weakSelf.historyTitleLabel.attributedText = attStr
            weakSelf.tableView.reloadData()
        }).disposed(by: rx.disposeBag)
    }
    
    override func bindViewAction() {
        saveBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.submitRemark(weakSelf.textView.text)
            weakSelf.backAction()
        }
    }
}

// MARK: - 扩展WmcCloudMineVC + UITableViewDelegate

extension WmcRemarkVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let model = viewModel.remarkHistoryRelay.value.yp.object(at: indexPath.row)
        {
            // 备注文字
            textView.text = model.remark
        }
    }
}

// MARK: - 扩展WmcCloudMineVC + UITableViewDataSource

extension WmcRemarkVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return .YP.Min
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return .YP.Min
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.remarkHistoryRelay.value.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcRemarkCell = tableView.yp.dequeueReusableCell()
        if let model = viewModel.remarkHistoryRelay.value.yp.object(at: indexPath.row)
        {
            cell.configModel(model)
        }else{
            cell.model = nil
        }
        cell.indexPath = indexPath
        cell.deleteRemarkBlock = { [weak self] indexPath, model in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.deleteRemark(indexPath, model: model)
        }
        
        return cell
    }
}
